﻿using EdiNet;
using Xunit;
using System;

namespace EdiNet.Test
{
    
    public class DataElementTest
    {




        /// <summary>
        ///A test for DataElement Constructor
        ///</summary>
        [Fact()]
        public void DataElementConstructorTest()
        {
            string value = "AAA"; 
            DataElement target = new DataElement(value);
            Assert.Equal(target[0], value) ;
            Assert.True(target.IsSimpleDataElement());
            Assert.Equal(target.Count(), 1);
        }

        /// <summary>
        ///A test for DataElement Constructor
        ///</summary>
        [Fact()]
        public void DataElementConstructorTest1()
        {
            DataElement target = new DataElement();
            Assert.Equal(target[0], string.Empty);
            Assert.True(target.IsSimpleDataElement());
            Assert.Equal(target.Count(), 1);
        }

        /// <summary>
        ///A test for Add
        ///</summary>
        [Fact()]
        public void AddTest()
        {
            string elementValue1 = "123";
            DataElement target = new DataElement(elementValue1); 
            string elementValue2 = "456"; 
            target.Add(elementValue2);
            Assert.False(target.IsSimpleDataElement());
            Assert.Equal(target.Count(), 2);
            Assert.Equal(target[0], elementValue1);
            Assert.Equal(target[1], elementValue2);
        }

        /// <summary>
        ///A test for Item
        ///</summary>
        [Fact()]
        public void ItemTest()
        {
            string value1 = "AAA";
            DataElement target = new DataElement(value1); 
            int position = 3; 
            string expected = "BBB"; 
            string actual;
            target[position] = expected;
            actual = target[position];
            Assert.Equal(expected, actual);
            Assert.Equal(target[0], value1);
            Assert.Equal(target[1], string.Empty);
            Assert.Equal(target[2], string.Empty);
            Assert.False(target.IsSimpleDataElement());
            Assert.Equal(target.Count(), 4);
        }

        /// <summary>
        ///A test for Item
        ///</summary>
        [Fact()]
        public void ItemTest2()
        {
            string value1 = "AAA";
            DataElement target = new DataElement(value1);
            int position = 3;
            string expected = string.Empty;
            string actual;
            actual = target[position];
            Assert.Equal(expected, actual);
            Assert.True(target.IsSimpleDataElement());
            Assert.Equal(target.Count(), 1);
        }

        /// <summary>
        ///A test for DataElement Constructor
        ///</summary>
        [Fact()]
        public void DataElementConstructorTest2()
        {
            string[] elements = new string[]{"AA", "AB"}; 
            DataElement target = new DataElement(elements);
            Assert.Equal(target[0], elements[0]);
            Assert.Equal(target[1], elements[1]);
            Assert.False(target.IsSimpleDataElement());
            Assert.Equal(target.Count(), 2);
        }

        /// <summary>
        ///A test for ToString
        ///</summary>
        [Fact()]
        public void ToStringTest()
        {
            DataElement target = new DataElement("TEST"); 
            string expected = "TEST"; 
            string actual;
            actual = target.ToString();
            Assert.Equal(expected, actual);
        }

        /// <summary>
        ///A test for ToString
        ///</summary>
        [Fact()]
        public void ToStringTest1()
        {
            DataElement target = new DataElement("AA", "", "BB"); 
            char componentElementSeparator = '`'; 
            string expected = "AA``BB"; 
            string actual;
            actual = target.ToString(componentElementSeparator);
            Assert.Equal(expected, actual);
        }

        /// <summary>
        ///A test for op_Implicit
        ///</summary>
        [Fact()]
        public void op_ImplicitTest()
        {
            DataElement el = new DataElement("AA", "BB");
            DataElement el2 = new DataElement("AA", "CC");
            string expected = "AA"; 
            string actual;
            actual = el;
            Assert.Equal(expected, actual);
            Assert.NotEqual(el, el2); 
            Assert.False(el == el2);
            Assert.True(el.V == el2);
            Assert.True(el == el2.V);
        }

        /// <summary>
        ///
        ///</summary>
        [Fact()]
        public void NullValueMustBeConvertedToEmptyString()
        {
            DataElement el = new DataElement((string)null);
            Assert.Equal(el.V, string.Empty);
            el.V = null;
            Assert.Equal(el.V, string.Empty);
            el[0] = null;
            Assert.Equal(el.V, string.Empty);
            el.Add(null);
            Assert.Equal(el[1], string.Empty);
        }


        /// <summary>
        ///A test for op_Equality
        ///</summary>
        [Fact()]
        public void op_EqualityTest()
        {
            DataElement el1 = new DataElement("AA");
            DataElement el2 = new DataElement("BB");
            Assert.False(el1 == el2);
        }

        /// <summary>
        ///A test for op_Inequality
        ///</summary>
        [Fact()]
        public void op_InequalityTest()
        {
            DataElement el1 = new DataElement("AA", "BB");
            DataElement el2 = new DataElement("AA", "BB");
            DataElement el3 = new DataElement("AA");
            Assert.False(el1 != el2);
            Assert.True(el1 != el3);
            Assert.True(el1 != null);
            Assert.True(null != el1);

        }

        /// <summary>
        ///A test for GetHashCode
        ///</summary>
        [Fact()]
        public void GetHashCodeTest()
        {
            DataElement el1 = new DataElement("AA", "BB"); 
            DataElement el2 = new DataElement("AA", "BB"); 
            Assert.True(el1.GetHashCode() == el2.GetHashCode());
        }

        /// <summary>
        ///A test for Equals
        ///</summary>
        [Fact()]
        public void EqualsTest()
        {
            DataElement el1 = new DataElement("AAA", "BBB");
            DataElement el2 = new DataElement("AAA", "BBB");
            DataElement el3 = new DataElement("AAA");
            DataElement el4 = new DataElement("AAA", "BBB", "DDD");
            DataElement el5 = new DataElement("AAA", "DDD");

            Assert.Equal(true, el1.Equals(el2));
            Assert.Equal(false, el1.Equals(el3));
            Assert.Equal(false, el1.Equals(el4));
            Assert.Equal(false, el1.Equals(el5));
            Assert.Equal(false, el1.Equals(null));
        }
    }
}
